package cn.edu.wynu;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * 通过xml配置文件配置
 */
public class TestQuery {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dataSource.xml");
        JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");

        // 查询字段
        String name = jdbcTemplate.queryForObject("select emp_name from t_emp where emp_id = 1", String.class);

        // 查询记录
        Employee employee = jdbcTemplate.queryForObject("select * from t_emp where emp_id = 1",
                new BeanPropertyRowMapper<>(Employee.class));

        // 查询列表
        List<Employee> employees = jdbcTemplate.query("select * from t_emp",
                new BeanPropertyRowMapper<>(Employee.class));

        System.out.println(name);
        System.out.println(employee);
        System.out.println(employees);
    }
}
